#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <mqueue.h>
#include <string.h>
int main(int argc, char const *argv[])
{
    char* name = "/my_mqueue";
    struct mq_attr attr;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 100;
    attr.mq_flags = 0;
    attr.mq_curmsgs = 0;
    mqd_t mqdes = mq_open(name,O_RDWR | O_CREAT,0664,&attr);

    if(mqdes == (mqd_t)-1){
        perror("mqopen");
        exit(EXIT_FAILURE);
    }

    pid_t pid = fork();
    if(pid == 0){
        sleep(1);
        char* buffer = (char*)malloc(attr.mq_msgsize);
        mq_receive(mqdes,buffer,attr.mq_msgsize,0);
        printf("I have received the %s\n",buffer);
    }else{
        char buffer[1024] = "Hi Yuki";
        mq_send(mqdes,buffer,strlen(buffer),0);
        printf("send successfully\n");
    }

    close(mqdes);

    if(pid > 0){
        mq_unlink(name);
    }
    return 0;
}
